home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / prog / cfuncs.zip / BEEP.C < prev    next >
C/C++ Source or Header  |  1992-01-17  |  2KB  |  69 lines

  1. #include <dos.h>
  2. #include <time.h>
  3. /*-----------------------------Beep --------------------------------*/
  4. /*                                    */
  5. /* DESCRIPTION: Sounds the speaker for a time specified in          */
  6. /*              microseconds by duration at a pitch specified       */
  7. /*              in hertz by frequency.                             */
  8. /*                                                                  */
  9. /* RETURNS : Nothing                                                */
  10. /* USES :  Nothing                                                  *
  11. /*------------------------------------------------------------------*/
  12. void Beep( int frequency, int duration )
  13.  
  14. {
  15. /* Define procedure for DOS */
  16.     int control;
  17.     void Sleep( clock_t wait );
  18.  
  19.     /* If frequency is 0, Beep doesn't try to make a sound. It
  20.      * just sleeps for the duration.
  21.      */
  22.     if( frequency )
  23.     {
  24.     /* 75 is about the shortest reliable duration of a sound.
  25.         if( duration < 75 )
  26.         duration = 75;
  27.     */
  28.  
  29.         /* Prepare timer by sending 10111100 to port 43. */
  30.         outp( 0x43, 0xb6 );
  31.  
  32.         /* Divide input frequency by timer ticks per second and
  33.          * write (byte by byte) to timer.
  34.          */
  35.         frequency = (unsigned)(1193180L / frequency);
  36.         outp( 0x42, (char)frequency );
  37.         outp( 0x42, (char)(frequency >> 8) );
  38.  
  39.         /* Save speaker control byte. */
  40.         control = inp( 0x61 );
  41.  
  42.         /* Turn on the speaker (with bits 0 and 1). */
  43.         outp( 0x61, control | 0x3 );
  44.     }
  45.  
  46.     Sleep( (clock_t)duration );
  47.  
  48.     /* Turn speaker back on if necessary. */
  49.     if( frequency )
  50.         outp( 0x61, control );
  51.  
  52. }
  53.  
  54. /*----------------------------- Sleep ------------------------------*/
  55. /*                                    */
  56. /* DESCRIPTION: A routine to pause for a specified number of        */
  57. /*              microseconds                                        */
  58. /*                                                                  */
  59. /* RETURNS : Nothing                                                */
  60. /* USES :  Nothing                                                  */
  61. /*------------------------------------------------------------------*/
  62. void Sleep( clock_t wait )
  63. {
  64.     clock_t goal;
  65.  
  66.     goal = wait + clock();
  67.     while( goal >= clock() )
  68.         ;
  69. }